<?php
/**
 * 描述：船配-采购信息列表。
 * @final
 */
final class Exec_Ancillary_Procurement_List extends Exec_Ancillary_Procurement_Public
{
    public function Get()
    {      
        #获取参数
        $type=Functions::UrlArgs('type');

        #定义标量
        $data=NULL;
        $params=array('type'=>$type);
        $contentTitle=NULL;

        #执行动作
        switch($type)
        {
            case 'recommend':
                $contentTitle='采购推荐';
                $data=DB::GetData('select * from procurement where M_Recommend_Is=true order by M_Recommend_Datetime desc,Datetime desc limit ?,20');
            break;
            case 'shipyard_recommend':
                $contentTitle='船厂采购推荐';
                $data=DB::GetData('select * from procurement where M_ShipyardRecommend_Is=true order by M_Recommend_Datetime desc,Datetime desc limit ?,20');
            break;
            case 'search':
                $contentTitle='采购搜索';
                $province=trim(@$_GET['province']);
                $city=trim(@$_GET['city']);
                $year=trim(@$_GET['year']);
                $title=trim(Functions::UrlArgs('title'));
                $sqlParams=array();
                $sql='select * from procurement where 1=1 ';
                {
                    if(is_numeric($province)&&$province!='-1')
                    {
                        $sql=$sql.'and Account in(select Username from account where Province=?) ';
                        $sqlParams[]=$province;
                        $params['province']=$province;
                    }

                    if(is_numeric($city)&&$city!='-1')
                    {
                        $sql=$sql.'and Account in(select Username from account where City=?) ';
                        $sqlParams[]=$city;
                        $params['city']=$city;
                    }

                    if(is_numeric($year)&&$year!='-1')
                    {
                        $sql=$sql.'and year(Datetime)=? ';
                        $sqlParams[]=$year;
                        $params['year']=$year;
                    }
                    
                    if($title)
                    {
                        $sql=$sql.'and Title like ? ';
                        $sqlParams[]='%'.$title.'%';
                        $params['title']=urlencode($title);
                    }
                }
                $sql=$sql.'order by ID desc limit ?,20';
                $data=DB::PGetData($sql,$sqlParams);
            break;
            case 'latest':
                $contentTitle='最新采购';
                $data=DB::GetData('select * from procurement order by Datetime desc limit ?,20');
            break;
            case 'class':
                $id=Functions::UrlArgs('id');
                if(!is_numeric($id)){
                    exit('error');
                }
                $contentTitle=NULL;
                {
                    $tmp=DB::GetRowByID('category','Identifying:'.$id);
                    if($tmp){
                        $contentTitle=$tmp['Name'];
                    }
                }
                $data=DB::GetData('select * from procurement where ClassID='.$id.' order by ID desc limit ?,20');
            break;
            case 'subclass':
                $id=Functions::UrlArgs('id');
                if(!is_numeric($id)){
                    exit('error');
                }
                $contentTitle=NULL;
                {
                    $tmp=DB::GetRowByID('category','Identifying:'.$id);
                    if($tmp){
                        $contentTitle=$tmp['Name'];
                    }
                }
                $data=DB::GetData('select * from procurement where SubclassID='.$id.' order by ID desc limit ?,20');
            break;
        }

        #数据处理
        foreach($data['Record'] as &$row)
        {
            //获取企业数据
            $tmp=DB::PGetRows('select * from account where Username=?',array(
                $row['Account']
            ));
            if($tmp)
            {
                $row['Account']=$tmp[0];
                //是否VIP
                $tmp=DB::PGetRows('select * from account_vip where Account=?',array(
                    $row['Account']['Username']
                ));
                if($tmp){
                    $row['Account']['IsVIP']='TRUE';
                }
                else{
                    $row['Account']['IsVIP']='FALSE';
                }
                //类别
                $row['ClassName']=NULL;
                {
                    $tmp=DB::GetRowByID('category','Identifying:'.$row['ClassID']);
                    if($tmp){
                        $row['ClassName']=$tmp['Name'];
                    }
                }
                $row['SubclassName']=NULL;
                {
                    $tmp=DB::GetRowByID('category','Identifying:'.$row['SubclassID']);
                    if($tmp){
                        $row['SubclassName']=$tmp['Name'];
                    }
                }
            }
            //处理图片路径
            $row['ThumbnailPath']=Config::PATH_RELATIVE_ATTACHMENT_TEMP.'/'.$row['ThumbnailPath'];
        }

        #渲染输出
        View::Assign('Procurements',$data['Record']);        
        View::Assign('PageMenu',PagerFactory::Create('Common')->Fetch($data,array
        (
            'URL'=>'exec.php?action=ancillary_procurement_list','IsDetail'=>TRUE,
            'Parameters'=>$params,
            'Rewriter'=>($type=='search') ? NULL:new PagerRewrite('/procurement/list/','type,id','html')
        )));
        View::Assign('Params',$params);
        View::Assign('ContentTitle',$contentTitle);
        View::Display('ancillary/procurement/list.tpl');
    }
}
?>